package com.leaves.sys.core.aop;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.leaves.core.annotion.JobLog;
import com.leaves.core.consts.AopSortConstant;
import com.leaves.sys.modular.timer.entity.SysTimers;
import com.leaves.sys.modular.timer.entity.SysTimersLog;
import com.leaves.sys.modular.timer.service.SysTimersLogService;
import com.leaves.sys.modular.timer.service.SysTimersService;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;

import java.lang.reflect.Method;
import java.util.Date;

/**
 * @Author Chenweiwei
 * @Date 2021/10/25 13:47
 * @Version 1.0
 */

@Aspect
@Order(AopSortConstant.JOB_LOG_AOP)
public class JobLogAop {


    private static final Log log = Log.get();

    /**
     * 切入点
     *
     * @author leaves
     * @date 2020/7/24 17:42
     */
    @Pointcut("@annotation(com.leaves.core.annotion.JobLog)")
    private void jobLogPointcut() {
    }

    /**
     * 执行具体的包装过程
     *
     * @author leaves
     * @date 2020/7/24 17:44
     */
    @Around("jobLogPointcut()")
    public Object doJobLog(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        SysTimersLog timersLog = new SysTimersLog();
        //获取注解参数
        MethodSignature methodSignature = (MethodSignature) point.getSignature();
        Method method = methodSignature.getMethod();
        JobLog jobLog = method.getAnnotation(JobLog.class);
        String title = jobLog.title();
        timersLog.setJobDetail(title);

        timersLog.setJobId(null);
        String beanName = point.getTarget().getClass().getSimpleName();
        timersLog.setBeanName(beanName);
        //获取任务id
        if (ObjectUtil.isNotEmpty(beanName)){
            SysTimersService sysTimersService = SpringUtil.getBean(SysTimersService.class);
            QueryWrapper<SysTimers> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().like(SysTimers::getActionClass,beanName);
            SysTimers sysTimers = sysTimersService.getOne(queryWrapper);
            if (ObjectUtil.isNotNull(sysTimers)){
                timersLog.setJobId(sysTimers.getId());
            }
        }
        SysTimersLogService timersLogService = SpringUtil.getBean(SysTimersLogService.class);
        Object result = null;
        try {
            timersLog.setParams(null);
            timersLog.setStatus(0);
            timersLog.setError("");
            timersLog.setCurrentRunningTime(new Date());
            timersLog.setCreateTime(new Date());
            timersLog.setCreateUser(1265476890672672808L);
            timersLog.setUpdateTime(new Date());
            timersLog.setUpdateUser(1265476890672672808L);
            //执行方法
            result = point.proceed();
            //执行时长(毫秒)
            long time = System.currentTimeMillis() - beginTime;
            timersLog.setTimes((int) time);
        } catch (Exception e) {
            //任务执行总时长
            long time = System.currentTimeMillis() - beginTime;
            timersLog.setTimes((int) time);
            timersLog.setStatus(1);
            timersLog.setError(StringUtils.substring(e.toString(), 0, 2000));
        } finally {
            assert timersLogService != null;
            timersLogService.save(timersLog);
        }
        return result;
    }
}
